package light.head.controller.site;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.site.ParkRecruitDAO;
import light.head.dao.site.RecruitInfoDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.ParkRecruit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *	园区招聘
 */
@Controller
@RequestMapping(Url.PARKRECRUIT)
public class ParkRecruitController extends SupporterController{
	private ParkRecruitDAO prDAO;
	private RecruitInfoDAO piDAO;

	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.PARKRECRUIT_INIT);
	}

	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.POST,Parm.NAME, Parm.SORT, Parm.DELETED, Parm.STATUS);
		if (prDAO.findByMap(map, pb) == RESULT.SUCCESS){
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}		
		return new ModelAndView(Url.PARKRECRUIT_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (null != code) {
			ParkRecruit pr = prDAO.get(code);
			request.setAttribute(Parm.PARKRECRUIT, pr);
		}
		return new ModelAndView(Url.PARKRECRUIT_DEIT);
	}
	@RequestMapping(Url.BATCHEDIT)
	public ModelAndView batchEdit( HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.RECRUITBATCH_EDIT);
	}
	@RequestMapping(value = Url.SAVE)
	public void save(ParkRecruit pr,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(pr.getCode() != null){
			result=prDAO.upd(pr);
		}else{
			Date date=new Date();
			pr.setDeleted(false);
			pr.setCt(date);
			pr.setCu(cu_code(request));
			result=prDAO.add(pr);
		}
		if(result==RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	//批量添加
	@RequestMapping(value = Url.SAVEBATCH)
	public void saveBatch(ParkRecruit pr,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(pr!=null){
			for(int i = 0; i<pr.getList().size();i++){
				if(pr.getList().get(i).getPost()!=null){
					pr.getList().get(i).setDeleted(false);
					pr.getList().get(i).setCu(cu_code(request));
					pr.getList().get(i).setCt(new Date());
					pr.getList().get(i).setStatus(Byte.parseByte(Sys.S_ONE));
					result = prDAO.add(pr.getList().get(i));
				}
			}
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	@RequestMapping(value = Url.DELETE)
	public void delete(Integer code,Byte type,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(code!=null){
			ParkRecruit pr = prDAO.get(code);
			pr.setDeleted(true);
			result=prDAO.upd(pr);
		}
		if(result==RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	/**
	 * 辅助查询公司
	 */
	@RequestMapping(Url.QUERYRECRUITINIT)
	public ModelAndView queryRecruitInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.QUERYRECRUIT_INIT);
	}
	@RequestMapping(value = Url.QUERYRECRUITLIST)
	public ModelAndView queryRecruitList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.POST,Parm.NAME, Parm.DELETED);
		if (piDAO.findByMap(map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.QUERYRECRUIT_LIST);
	}
	//批量辅助查询
	@RequestMapping(Url.BATCHRECRUITINIT)
	public ModelAndView batchRecruitInit(Integer index, HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute("index", index);
		return new ModelAndView(Url.BATCHRECRUITINIT_INIT);
	}
	@RequestMapping(value = Url.BATCHBATCHRECRUITLIST)
	public ModelAndView batchRecruitList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.POST, Parm.NAME, Parm.DELETED);
		if (piDAO.findByMap(map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.BATCHBATCHRECRUIT_LIST);
	}
	
	//公司申请招聘信息首页展示
	@RequestMapping(value = Url.APPLYSHOW)
	public void applyRc(Integer code, Integer oc, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(code != null && oc != null) {
			ParkRecruit pr = new ParkRecruit();
			pr.setCt(new Date());
			pr.setDeleted(false);
			pr.setOc(oc);
			pr.setCu(cu_code(request));
			pr.setRc(code);
			pr.setStatus(Byte.parseByte(Sys.S_ZERO));
			result = piDAO.add(pr);
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	@Autowired
	public void setPrDAO(ParkRecruitDAO prDAO) {
		this.prDAO = prDAO;
	}
	@Autowired
	public void setPiDAO(RecruitInfoDAO piDAO) {
		this.piDAO = piDAO;
	}
}
